<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <!-- 注意：label决定当前选中的值 -->
      <el-checkbox v-for="item in List" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/setting'
import { getInfoById, assignRoles } from '@/api/user'
export default {
  props: {
    userId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      List: []
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    resetForm() {
      this.roleIds = []
    },
    async loadRoles() {
      try {
        const res = await getRoles({ page: 1, pagesize: 100 })
        // 通过Id获取用户信息
        const resInfo = await getInfoById(this.userId)
        console.log(resInfo, 'resInfo')
        this.roleIds = resInfo.data.roleIds
        console.log(res)
        this.List = res.data.rows
      } catch (error) {
        console.log(error)
      }
    },
    async hSubmit() {
      try {
        const res = await assignRoles({ id: this.userId, roleIds: this.roleIds })
        console.log(res, 'hSubmit')
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    closeDialog() {
      // 清空勾选
      this.$emit('close')
    }
  }
}
</script>
